//
//  CSSectionListViewController.m
//  ChinaShip
//
//  Created by yyk on 2019/1/24.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSSectionListViewController.h"
#import "CSSectionDetailViewController.h"
#import "CSSectionListStyleViewController.h"
#import <TYCyclePagerView/TYCyclePagerView.h>
#import "CSHomeSlideCollectionViewCell.h"
#import "CSSecondVCBottomView.h"
#import "CSSecondListApi.h"
#import "CSSecondModel.h"
#import <WXSTransition/UINavigationController+WXSTransition.h>
@interface CSSectionListViewController ()<TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>
@property (nonatomic, strong) CSSecondVCBottomView          *secondBottomView;
@property (nonatomic, strong) TYCyclePagerView              *pagerView;
@property (nonatomic, strong) UIView                        *coverView;
@property (nonatomic, strong) NSMutableArray                *showImages,*titles;
@property (nonatomic, strong) UILabel                       *pageLabel;
@property (nonatomic, strong) NSMutableArray                *secondModels;
@property (nonatomic,   weak) IBOutlet NSLayoutConstraint   *wLayoutConstraint;
@property (nonatomic, assign) BOOL                           isShow;
@end

@implementation CSSectionListViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    if (self.isShow) {
//        self.wLayoutConstraint.constant = 64;
//        [self.view layoutIfNeeded];
//    }else{
//        self.wLayoutConstraint.constant = 10;
//        [self.view layoutIfNeeded];
//    }
    [self showNavBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackNavbarItem];
    // Do any additional setup after loading the vie
    [self getSecondList];
}

//二次
- (void)getSecondList{
    [SVProgressHUD show];
    CSSecondListApi *secondListApi = [CSSecondListApi new];
    secondListApi.mid = self.mid+1;
    [secondListApi loadRequestComplete:^(TBURLResponse *response, NSError *error) {
        CSResponse *_response = response.returnObject;
        if ([_response.status integerValue] == 0) {
            [SVProgressHUD dismiss];
             NSArray *secondModelList = [CSSecondModel  entityListWithData:_response.data entityClass:[CSSecondModel class]];
            for (CSSecondModel *itemModel in secondModelList) {
                [self.showImages addObject:ImageUrl(itemModel.cover)];
                [self.titles addObject:itemModel.name];
            }
            [self.secondModels addObjectsFromArray:secondModelList];
            [self addPageView];
        }else{
            [SVProgressHUD showErrorWithStatus:_response.msg];
        }
    }];
}

- (NSMutableArray *)showImages{
    if (!_showImages) {
        _showImages = [NSMutableArray new];
    }
    return _showImages;
}

- (NSMutableArray *)titles{
    if (!_titles) {
        _titles = [NSMutableArray new];
    }
    return _titles;
}

- (NSMutableArray *)secondModels{
    if (!_secondModels) {
        _secondModels = [NSMutableArray new];
    }
    return _secondModels;
}

- (void)addPageView{
    TYCyclePagerView *pageView = [TYCyclePagerView new];
    pageView.backgroundColor = [UIColor clearColor];
    pageView.isInfiniteLoop = YES;
    pageView.dataSource = self;
    pageView.delegate = self;
    [pageView registerNib:[UINib nibWithNibName:@"CSHomeSlideCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"homeSlide_cell"];
    [self.view addSubview:pageView];
    _pagerView = pageView;
    
    self.coverView = [UIView new];
    self.coverView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.coverView];
    
    UISwipeGestureRecognizer *swipLeftGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureAction:)];
    [swipLeftGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.coverView addGestureRecognizer:swipLeftGestureRecognizer];

    UISwipeGestureRecognizer *swipRightGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureAction:)];
    [swipRightGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.coverView addGestureRecognizer:swipRightGestureRecognizer];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
    [self.coverView addGestureRecognizer:tapGesture];
    
    self.pageLabel = [[UILabel alloc]init];
    self.pageLabel.font = [UIFont systemFontOfSize:16];
    self.pageLabel.textColor = [UIColor blackColor];
    self.pageLabel.textAlignment = NSTextAlignmentCenter;
    self.pageLabel.text = [NSString stringWithFormat:@"1/%ld",self.showImages.count];
    [self.view addSubview:self.pageLabel];
    
    if (self.secondModels.count == 0) {
        self.pageLabel.text = @"";
    }
    
    CGFloat y = kSCREEN_HEIGHT-150 + 50;
    __weak typeof(self)weakSelf = self;
    self.secondBottomView = [CSSecondVCBottomView createBottomViewWithFame:CGRectMake(0,y, kSCREEN_WIDTH, kSCREEN_HEIGHT-y) datas:self.showImages];
    self.secondBottomView.moveDirectionBlock = ^(UISwipeGestureRecognizer *swipeGesture,NSInteger moveCount){
        if (moveCount == 100000) {
            if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
                [weakSelf.pagerView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionRight animate:YES];
            }else{
                [weakSelf.pagerView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionLeft animate:YES];
            }
        }else{
            [weakSelf.pagerView scrollToItemAtIndex:moveCount animate:NO];
        }
    };
    [self.view addSubview:self.secondBottomView];
    
}

//单击
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)tap{
    if (self.secondModels.count == 0) {
        return;
    }
    CGSize itemSize = CGSizeZero;
    if (isIPhonex) {
        itemSize = CGSizeMake(CGRectGetWidth(_pagerView.frame)*0.7, 450);
    }else{
        itemSize = CGSizeMake(CGRectGetWidth(_pagerView.frame)*0.7, CGRectGetHeight(_pagerView.frame)-10);
    }
    CGRect jRect = CGRectMake((kSCREEN_WIDTH-itemSize.width)/2.0, (CGRectGetHeight(_pagerView.frame)-itemSize.height)/2, itemSize.width, itemSize.height);
    if (CGRectContainsPoint(jRect, [tap locationInView:tap.view])) {
        CSSecondModel *secondModel = self.secondModels[self.pagerView.curIndex];
        CSSectionDetailViewController *sectionDetailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CSSectionDetailViewController"];
        sectionDetailVC.mid = [secondModel.id integerValue];
        [self.navigationController wxs_pushViewController:sectionDetailVC makeTransition:^(WXSTransitionProperty *transition) {
            transition.animationTime = 0.4;
            transition.isSysBackAnimation = YES;
            transition.animationType = WXSTransitionAnimationTypeBrickOpenVertical;
            self.isShow = YES;
        }];
    }
}





//侧滑
- (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipeGesture{
    if (self.showImages.count <= 1) {
        return;
    }
    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {
        [self.pagerView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionRight animate:YES];
    }else{
        [self.pagerView scrollToNearlyIndexAtDirection:TYPagerScrollDirectionLeft animate:YES];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:kMoveDirectionNotifation object:swipeGesture];
}


- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    _pagerView.frame = CGRectMake(0, 120, kSCREEN_WIDTH, kSCREEN_HEIGHT-150-120);
    self.coverView.frame = _pagerView.frame;
    if (isIPhonex) {
        self.pageLabel.frame = CGRectMake(0, CGRectGetMaxY(_pagerView.frame)-(CGRectGetHeight(_pagerView.frame)-450)/2, kSCREEN_WIDTH, 80);
    }else{
        self.pageLabel.frame = CGRectMake(0, CGRectGetMaxY(_pagerView.frame), kSCREEN_WIDTH, 50);
    }
}


#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.showImages.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    CSHomeSlideCollectionViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"homeSlide_cell" forIndex:index];
    cell.layer.cornerRadius = 10.0f;
    [cell setCoverImageURL:self.showImages[index] title:self.titles[index]];
    [cell setSecondDetailStyle];
    return cell;
}

//点击跳转界面
- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index{
//    [self pushVcToOtherVc:[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CSSectionListViewController"]];
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    if (isIPhonex) {
        layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)*0.7, 450);
    }else{
        layout.itemSize = CGSizeMake(CGRectGetWidth(pageView.frame)*0.7, CGRectGetHeight(pageView.frame)-10);
    }
    layout.itemSpacing = CGRectGetWidth(pageView.frame)*0.15;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    self.pageLabel.text = [NSString stringWithFormat:@"%ld/%ld",toIndex+1,self.showImages.count];
}




//more
- (IBAction)lookMoreAction:(id)sender{
    self.isShow = NO;
    CSSectionListStyleViewController *sectionListVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"CSSectionListStyleViewController"];
    sectionListVC.modelsList = self.secondModels;
    [self pushVcToOtherVc:sectionListVC];
}
//contactUs
-(IBAction)contactUsAction:(id)sender{
    self.isShow = NO;
    [self pushVcToOtherVc:[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CSContactUsViewController"]];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
